import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class FrameTest extends JFrame implements ActionListener, ChangeListener
{
	private JPanel buttonPanel;
	private JPanel sliderPanel;
	
	public FrameTest()
	{
		//super("FrameTest");
		
		try
		{
			// Set System L&F
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		}
		catch (UnsupportedLookAndFeelException e)
		{
			e.printStackTrace();
		}
		catch (ClassNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (InstantiationException e)
		{
			e.printStackTrace();
		}
		catch (IllegalAccessException e)
		{
			e.printStackTrace();
		}
		
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setIconImage(new ImageIcon("images\\wndIcon.png").getImage());
		
		buttonPanel = new JPanel();
	
		JButton button = new JButton("Push Me!");
		button.setMnemonic(KeyEvent.VK_P);
		button.setToolTipText("Click This!");
		button.setActionCommand("Switch Panel");
		button.addActionListener(this);
		
		buttonPanel.add(button);
		this.add(buttonPanel);
		
		this.pack();
		this.setLocationRelativeTo(null);
		this.setVisible(true);
	}
	
	public static void main(String[] args)
	{
		JFrame frame = new FrameTest();
	}
	
	public void switchToSlider()
	{
		buttonPanel.setVisible(false);
		this.setSize(300, 400);
		
		JSlider gradient = new JSlider(JSlider.HORIZONTAL, 0, 256, 128);
		// the slider goes to 256 for convenience of tick marks, but it does cap
		// at 255 for the color value
		gradient.setMajorTickSpacing(64);
		gradient.setMinorTickSpacing(16);
		gradient.setPaintTicks(true);
		gradient.setPaintLabels(true);
		gradient.addChangeListener(this);
		
		sliderPanel = new JPanel();
		sliderPanel.setSize(300, 400);
		sliderPanel.setBackground(new Color(127, 127, 127));
		sliderPanel.add(gradient);
		this.add(sliderPanel);	
	}
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		if ("Switch Panel".equals(e.getActionCommand()))
		{
			this.switchToSlider();
		}
		
	}
	
	@Override
	public void stateChanged(ChangeEvent e)
	{
		JSlider source = (JSlider)e.getSource();
		
		// if (!source.getValueIsAdjusting())
		// {
		int colorInt = source.getValue();
		if (colorInt > 255)
		{
			colorInt = 255;
		}
		sliderPanel.setBackground(new Color(colorInt, colorInt, colorInt));
		// }
		// System.out.println("State Changed");
	}	
}
